package com.guaoran.interview.spring.aop.controller;

import com.guaoran.interview.spring.aop.service.JdkProxyServiceImpl;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author : 孤傲然
 * @description : jdk 和 cglib
 * @desc ： 当我们使用jdk动态代理时，如果实现类实现了接口并继承了某个类， 则生成的代理类无法对 继承的类进行代理。
 * @date :2020/1/7 13:45
 */
@RestController
public class JDKProxyAndCglibController {


    public static void main(String[] args) {
        Class clazz = new JdkProxyServiceImpl().getClass();
        String proxyName = "$Proxy";
        byte[] classFile = ProxyGenerator.generateProxyClass(proxyName, clazz.getInterfaces());
        String paths = clazz.getResource(".").getPath();
        System.out.println(paths);
        FileOutputStream out = null;
        try {
            //保留到硬盘中
            out = new FileOutputStream(paths + proxyName + ".class");
            out.write(classFile);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
